package com.elai.baseconfig.controller;

import java.util.List;

import com.elai.baseconfig.domain.BaseLesseeInfo;
import com.elai.baseconfig.domain.EventTeamDO;
import com.elai.baseconfig.service.IBaseLesseeInfoService;
import com.elai.common.core.domain.entity.SysUser;
import com.elai.common.utils.DateUtils;
import com.elai.common.utils.SecurityUtils;
import com.elai.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.enums.BusinessType;
import com.elai.baseconfig.domain.SysDatabaseSync;
import com.elai.baseconfig.service.ISysDatabaseSyncService;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.common.core.page.TableDataInfo;

/**
 * 数据库同步Controller
 *
 * @author elai
 * @date 2022-05-07
 */
@RestController
@RequestMapping("/baseconfig/dbsync")
public class SysDatabaseSyncController extends BaseController
{
    @Autowired
    private ISysDatabaseSyncService sysDatabaseSyncService;

    @Autowired
    private IBaseLesseeInfoService baseLesseeInfoService;

    /**
     * 查询数据库同步列表
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:dbsync:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDatabaseSync sysDatabaseSync)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            sysDatabaseSync.setLesseeId(currentUser.getLesseeId());
        }
        startPage();
        List<SysDatabaseSync> list = sysDatabaseSyncService.selectSysDatabaseSyncList(sysDatabaseSync);
        return getDataTable(list);
    }

    /**
     * 导出数据库同步列表
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:dbsync:export')")
    @Log(title = "数据库同步", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysDatabaseSync sysDatabaseSync)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.isAdmin()){
            sysDatabaseSync.setLesseeId(currentUser.getLesseeId());
        }
        List<SysDatabaseSync> list = sysDatabaseSyncService.selectSysDatabaseSyncList(sysDatabaseSync);
        ExcelUtil<SysDatabaseSync> util = new ExcelUtil<SysDatabaseSync>(SysDatabaseSync.class);
        return util.exportExcel(list, "数据库同步数据");
    }

    /**
     * 获取数据库同步详细信息
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:dbsync:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysDatabaseSyncService.selectSysDatabaseSyncById(id));
    }

    /**
     * 新增数据库同步
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:dbsync:add')")
    @Log(title = "数据库同步", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDatabaseSync sysDatabaseSync)
    {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(StringUtils.isNotEmpty(currentUser.getLesseeId())){
            BaseLesseeInfo lesseeInfo= baseLesseeInfoService.selectBaseLesseeInfoByLesseeId(currentUser.getLesseeId());
            if(lesseeInfo==null){
                return AjaxResult.error("添加'" + sysDatabaseSync.getName() + "'失败，租户ID不存在");
            }
            if("02".equals(lesseeInfo.getStatus())){
                return AjaxResult.error("添加'" + sysDatabaseSync.getName() + "'失败，租户异常请联系管理员");
            }
            if(lesseeInfo.getBeginDate().getTime()> DateUtils.getNowDate().getTime() || DateUtils.getNowDate().getTime()>lesseeInfo.getEndDate().getTime()){
                return AjaxResult.error("添加'" + sysDatabaseSync.getName() + "'失败，租户不在有效期内，请联系管理员");
            }
            sysDatabaseSync.setLesseeId(currentUser.getLesseeId());
        }else{
            return AjaxResult.error("添加'" + sysDatabaseSync.getName()   + "'失败，请联系管理员配置租户");
        }
        return toAjax(sysDatabaseSyncService.insertSysDatabaseSync(sysDatabaseSync));
    }

    /**
     * 修改数据库同步
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:dbsync:edit')")
    @Log(title = "数据库同步", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDatabaseSync sysDatabaseSync)
    {
        return toAjax(sysDatabaseSyncService.updateSysDatabaseSync(sysDatabaseSync));
    }

    /**
     * 删除数据库同步
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:dbsync:remove')")
    @Log(title = "数据库同步", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDatabaseSyncService.deleteSysDatabaseSyncByIds(ids));
    }
}
